#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年10月8日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]


import sys,os,logging
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

'''
    在docker 容器中 批量设置 kafka 、zookeeper 配置
'''

import paramiko




def run_cmd_in_remote(ip='localhost',port=22,cmd=''):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip,port,"root", "zhimatech")
    
    stdin , stdout , stderr = ssh.exec_command(cmd)
    hostname =  stdout.read() 
    return hostname.strip()

def create_zookeeper_myid(zookeeper_ips=[],myidfile='/tmp/zookeeper/myid'):
    i=0
    
    for zookeeper_str in zookeeper_ips:
        i+=1
        ip,port = zookeeper_str.split(':')
        cmd = 'echo "%s" > %s'%(i,myidfile)
        logging.info(cmd)
        run_cmd_in_remote(ip=ip,port=int(port),cmd='mkdir /tmp/zookeeper/')
        run_cmd_in_remote(ip=ip,port=int(port),cmd=cmd)
        
        
def mofify_zookeeper_config(zookeeper_ips=[],zookeeper_hosts=[],configfile='/opt/zookeeper/conf/zoo.cfg'):
     for zookeeper_str in zookeeper_ips:
        ip,port = zookeeper_str.split(':')
        port = int(port)
        i=0
        for hostname in zookeeper_hosts:
            i+=1
            #hostname = zookeeper_hosts[i-1]
            #hostname = run_cmd_in_remote(ip=ip,port=port,cmd='hostname')
            
            cmd = "sed -i.bak 's/^server.%s=.*/server.%s=%s:2888:3888/g' %s"%(i,i,hostname,configfile)
            logging.info(cmd)
            run_cmd_in_remote(ip=ip,port=port,cmd=cmd)
        
def mofdify_kafka_config(kafka_ips=[],configfile='/opt/kafka/config/server.properties',fields={'broker.id':None,'host.name':None}):
    '''
        修改 broker.id
        修改 host.name
        
        #sed -i.bak 's/^broker.id=.*/broker.id=1/g' server.properties.bak
    '''
    i=-1
    
    for kafka_str in kafka_ips:
        i+=1
        ip,port = kafka_str.split(':')
        port =int(port)
        for field_name,field_value in fields.items():
            if field_name == 'broker.id':
                field_value = i
            elif field_name == 'host.name':
                field_value = run_cmd_in_remote(ip=ip,port=port,cmd="ifconfig eth0 | grep 'inet addr' | awk '{ print $2}' | awk -F: '{print $2}'")
            
            cmd = "sed -i.bak 's/^%s=.*/%s=%s/g' %s"%(field_name,field_name,field_value,configfile)
            logging.info(cmd)
            run_cmd_in_remote(ip=ip,port=port,cmd=cmd)
        
        
logging.basicConfig(level=logging.INFO)


zookeeper_ips = [
                '192.168.1.208:12022',
                '192.168.1.208:12122',
                '192.168.1.208:12222'
                ]

kafka_ips = [
             '192.168.1.208:12022',
             '192.168.1.208:12122',
             '192.168.1.208:12222',
             '192.168.1.208:12322'
             ]


zookeeper_hosts = [
                   'sz120',
                   'sz121',
                   'sz122'
                   ]

create_zookeeper_myid(zookeeper_ips = zookeeper_ips)
mofify_zookeeper_config(zookeeper_ips = zookeeper_ips,zookeeper_hosts=zookeeper_hosts)
mofdify_kafka_config(kafka_ips=kafka_ips,fields={'broker.id':None,'host.name':None,'zookeeper.connect':'sz120:2181,sz121:2181,sz122:2181'})
